package com.supermap.geometrist.toolkit.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supermap.data.*;
import com.supermap.geometrist.toolkit.modle.ResponseText;
import com.supermap.services.rest.HttpError;

public class GeometristUtils {


    public static String geometristMethod(JSONObject paramJSON) {

        JSONObject jsoReturn = new JSONObject();
        ResponseText responseText = new ResponseText();
        JSONObject search = paramJSON.getJSONObject("geometrySearch");
        JSONObject target = paramJSON.getJSONObject("geometryTarget");

        GeometristTools tools = new GeometristTools();
        String operation = paramJSON.getString("operation");

        Geometry geometrySearch = tools.analysis(search);
        Geometry geometryTarget = tools.analysis(target);

        if (geometrySearch == null || geometryTarget == null || operation == null) {
            responseText.setSucceed(false);
            HttpError error = new HttpError();
            error.setErrorMsg("Parameter error, please check the parameter");
            responseText.setError(error);
            return JSONObject.toJSONString(responseText);
        }

        switch (operation) {
            case "disjoint":
                boolean isDisjointed = Geometrist.isDisjointed(geometrySearch, geometryTarget);
                jsoReturn.put("isDisjointed", isDisjointed);
                break;
            case "touches":
                boolean hasTouch = Geometrist.hasTouch(geometrySearch, geometryTarget);
                jsoReturn.put("hasTouch", hasTouch);
                break;
            case "crosses":
                if (geometrySearch.getType().toString().equals("GEOLINE") && !geometryTarget.getType().toString().equals("GEOPOINT")) {
                    boolean hasCross = Geometrist.hasCross(geometrySearch, geometryTarget);
                    jsoReturn.put("hasCross", hasCross);
                } else {
                    responseText.setSucceed(false);
                    HttpError error = new HttpError();
                    error.setErrorMsg("Parameter error, please check the parameter");
                    responseText.setError(error);
                    return JSONObject.toJSONString(responseText);
                }
                break;
            case "contains":
                boolean canContain = Geometrist.canContain(geometrySearch, geometryTarget);
                jsoReturn.put("canContain", canContain);
                break;
            case "overlaps":
                if (geometrySearch.getType().toString().equals("GEOPOINT") && geometryTarget.getType().toString().equals("GEOPOINT")) {
                    responseText.setSucceed(false);
                    HttpError error = new HttpError();
                    error.setErrorMsg("Parameter error, please check the parameter");
                    responseText.setError(error);
                    return JSONObject.toJSONString(responseText);
                } else {
                    boolean hasOverlap = Geometrist.hasOverlap(geometrySearch, geometryTarget);
                    jsoReturn.put("hasOverlap", hasOverlap);
                }
                break;
            default:
                responseText.setSucceed(false);
                HttpError error = new HttpError();
                error.setErrorMsg("Parameter error, please check the parameter");
                responseText.setError(error);
                return JSONObject.toJSONString(responseText);
        }
        return jsoReturn.toString();
    }
}
